package gdconf

import (
	"server_cluster/common/logger"
)

// 武器等阶配置表

type WeaponPromoteConfig struct {
	Id              int32            `csv:"Id" gorm:"primary_key;autoIncrement:false"` // 武器等级id
	PromoteGroup    int32            `csv:"PromoteGroup,omitempty"`                    // 升阶组
	Promote         int32            `csv:"Promote,omitempty"`                         // 等阶
	LevelLimit      int32            `csv:"LevelLimit,omitempty"`                      // 解锁等级上限
	NeedPlayerLevel int32            `csv:"PlayerLevel,omitempty"`                     // 所需玩家等级
	FightPropList   FightPropAddList `csv:"Attribute,omitempty"`                       // 战斗属性列表
	CostItemList    ItemList         `csv:"Material,omitempty"`                        // 所需材料
	Gold            int32            `csv:"GoldCoin,omitempty"`                        // 消耗金币
}

func (w *WeaponPromoteConfig) TableName() string {
	return "config_weapon_promote"
}

func (g *GameDataConfig) saveWeaponPromoteConfig() {
	saveTableToDb[WeaponPromoteConfig](g.Db, readTable[WeaponPromoteConfig](g.CsvPathPrefix+"Weaponpromote.csv"))
}

func (g *GameDataConfig) loadWeaponPromoteConfig() {
	g.GameDataMaps.WeaponPromoteConfigMap = make(map[int32]map[int32]*WeaponPromoteConfig)
	for _, weaponPromoteConfig := range loadTableFromDb[WeaponPromoteConfig](g.Db) {
		_, exist := g.GameDataMaps.WeaponPromoteConfigMap[weaponPromoteConfig.PromoteGroup]
		if !exist {
			g.GameDataMaps.WeaponPromoteConfigMap[weaponPromoteConfig.PromoteGroup] = make(map[int32]*WeaponPromoteConfig)
		}
		g.GameDataMaps.WeaponPromoteConfigMap[weaponPromoteConfig.PromoteGroup][weaponPromoteConfig.Promote] = weaponPromoteConfig
	}
	logger.Info("WeaponPromoteConfig Count: %v", len(g.GameDataMaps.WeaponPromoteConfigMap))
}

func GetWeaponPromoteConfigByGroupAndPromote(group int32, promote int32) *WeaponPromoteConfig {
	value1, exist := CONF.GameDataMaps.WeaponPromoteConfigMap[group]
	if !exist {
		logger.Error("[ConfigNotFound] WeaponPromoteConfig, group: %v, promote: %v", group, promote)
		return nil
	}
	value2, exist := value1[promote]
	if !exist {
		logger.Error("[ConfigNotFound] WeaponPromoteConfig, group: %v, promote: %v", group, promote)
		return nil
	}
	return value2
}

func GetWeaponPromoteConfigMap() map[int32]map[int32]*WeaponPromoteConfig {
	return CONF.GameDataMaps.WeaponPromoteConfigMap
}
